package com.example.test;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.Manifest;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Fragment fragment1 = new BlankFragment1();
    Fragment fragment2 = new BlankFragment2();
    Fragment fragment3 = new BlankFragment3();
    Fragment fragment4 = new BlankFragment4();

    private static final int SEND_SMS = 100;
    private static final int RECEIVE_SMS = 101;

    private static final String SMS_SENT_ACTION = "SMS_SENT";
    private static final String SMS_RECEIVED = "SMS_RECEIVED";

    private static final String message = "这是一条测试短信";

    private static final String phoneNumber = "19372436692";

    private FragmentManager manager;

//    接收器
    MyBroadcastReceiver br;

//    短信权限码


    LinearLayout linearLayout1, linearLayout2, linearLayout3, linearLayout4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);


        linearLayout1 = findViewById(R.id.lay1);
        linearLayout2 = findViewById(R.id.lay2);
        linearLayout3 = findViewById(R.id.lay3);
        linearLayout4 = findViewById(R.id.lay4);

        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);


        initFragment();
        showfragment(0);
    }

    @Override
    protected void onStart() {
        super.onStart();
        br = new MyBroadcastReceiver();
//        过滤不满足条件的过滤器
        IntentFilter filter = new IntentFilter(MyBroadcastReceiver.STANDARD_ACTION);
//        注册接收器
        registerReceiver(br, filter);
        requestPermission();
    }

    @Override
    protected void onStop() {
        super.onStop();
//        注销
        unregisterReceiver(br);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initFragment() {
        manager = getSupportFragmentManager();
        androidx.fragment.app.FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.content, fragment1);
        transaction.add(R.id.content, fragment2);
        transaction.add(R.id.content, fragment3);
        transaction.add(R.id.content, fragment4);
        transaction.commit();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.lay1) {
            showfragment(0);
        }
        else if(id == R.id.lay2) {
            showfragment(1);
        }
        else if(id == R.id.lay3) {
            showfragment(2);
        }
        else if(id == R.id.lay4) {
            showfragment(3);
        }
    }

    public void showfragment(int i) {
        FragmentTransaction transaction = manager.beginTransaction();
        hideFragment(transaction);
        if(i == 0) {
            transaction.show(fragment1);
        }
        else if (i == 1) {
            transaction.show(fragment2);
        }
        else if(i == 2) {
            transaction.show(fragment3);
        }
        else if (i == 3) {
            transaction.show(fragment4);
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        transaction.hide(fragment1);
        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.hide(fragment4);
    }

//    权限申请
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == SEND_SMS){
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                sendSMS();
            }
            else {
                Toast.makeText(MainActivity.this, "SEND_SMS 不认可", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void requestPermission() {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SEND_SMS);
    }


    public boolean sendSMS() {
        SmsManager manager = SmsManager.getDefault();
        ArrayList<String> strings = manager.divideMessage(message);
        for (int i = 0;i < strings.size();i++) {
            boolean sented = false;
            try {
                manager.sendTextMessage(phoneNumber, null, message, null, null);
                sented = true;
            }catch (Exception e) {
                e.printStackTrace();
            }
            if (sented) {
                Log.d("xr", "消息发送成功");
            }else {
                Log.d("xr", "消息发送失败");
            }
        }
        return true;
    }
}